Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

159
Visualizações
How to add one hour for "24 hours" Time format in javascript/jquery?

I want to add one hour in 24 hours time format using Javascript or JQuery.

like

if 23:00 then it should be 00:00
if 11:00 then 12:00
if 13:00 then 14:00 if 09:00 then 10:00

for that I tried below code. That's perfectly works

    function increase24TimeByOne(timeStr) {
            var splitedTimeStr = timeStr.split(':');
            var hours = parseInt(splitedTimeStr[0]);
            var minutes = splitedTimeStr[1].substring(0, 2);
            var nextHours = (hours + 1);
          
            if(nextHours.toString().length == 1){
                nextHours = "0" + nextHours;
            }
            if(nextHours == 24){
                nextHours = "00"
            }
            return nextHours + ":" + minutes;
        }

but I want to know that code is better or not ?
Can anyone please suggest ?

Thanks in advance

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Using modulo maths add 1 modulo 24

i.e. (23 + 1) % 24 === 0

function increase24TimeByOne(timeStr) {
  var splitedTimeStr = timeStr.split(':');
  var hours = parseInt(splitedTimeStr[0]);
  var minutes = splitedTimeStr[1].substring(0, 2);
  var nextHours = (hours + 1) % 24;
  var nextMeridiem;
  if (nextHours.toString().length == 1) {
    nextHours = "0" + nextHours;
  }
  return nextHours + ":" + minutes;
}

console.log(increase24TimeByOne('23:45'))

You can do better, by converting to string AND padding (optionally) the '0' at the same time

using toString().padStart(2, '0') - i.e add '0' until the string is at least 2 in length

function increase24TimeByOne(timeStr) {
    var splitedTimeStr = timeStr.split(':');
    var hours = parseInt(splitedTimeStr[0]);
    var minutes = splitedTimeStr[1].substring(0, 2);
    var nextHours = ((hours + 1) % 24).toString().padStart(2, '0');
    return nextHours + ":" + minutes;
}
console.log(increase24TimeByOne('23:45'))

In my opinion, you can do this with even less code by using string templates

It may not be so readable though, but less code is better code (to a point)

function increase24TimeByOne(timeStr) {
    let [hours, minutes] = timeStr.split(':');
    return `${((+hours + 1) % 24).toString().padStart(2, '0')}:${minutes}`;
}
console.log(increase24TimeByOne('23:45'))

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda